package com.example.demo.controller;

import com.example.demo.entity.Organization;
import com.example.demo.entity.Organization.OrgType;
import com.example.demo.repository.OrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/organizations")
@CrossOrigin
public class OrganizationController {

    @Autowired
    private OrganizationRepository organizationRepository;

    /**
     * 获取所有组织结构（学院/专业/班级）
     */
    @GetMapping
    public List<Organization> getAllOrganizations() {
        return organizationRepository.findAll();
    }

    /**
     * 获取所有学院
     */
    @GetMapping("/colleges")
    public List<Organization> getAllColleges() {
        return organizationRepository.findByType(OrgType.college);
    }

    /**
     * 获取某学院下的专业
     */
    @GetMapping("/majors/{collegeId}")
    public List<Organization> getMajorsByCollege(@PathVariable Long collegeId) {
        return organizationRepository.findByParentIdAndType(collegeId, OrgType.major);
    }

    /**
     * 获取某专业下的班级
     */
    @GetMapping("/classes/{majorId}")
    public List<Organization> getClassesByMajor(@PathVariable Long majorId) {
        return organizationRepository.findByParentIdAndType(majorId, OrgType.clazz);
    }
}
